//
//  WBStatusCell.swift
//  SwiftWeibo
//
//  Created by Lausen on 2017/4/24.
//  Copyright © 2017年 id. All rights reserved.
//

import UIKit

@objc protocol WBStatusCellDelegate: NSObjectProtocol {
    
    @objc optional func didSelectedStatusCell(statusCell: WBStatusCell, url: String)
}

class WBStatusCell: UITableViewCell {

    /// 定义一个代理的变量
    weak var delegate: WBStatusCellDelegate?
    
    var viewModel: WBStatusViewModel?{
        
        didSet{
            
            /// 原创微博的属性文字
            statusLabel!.attributedText = viewModel?.statusAttrText
            
            /// 转发微博的属性文字
            reteewedLabel?.attributedText = viewModel?.reteewtedAttrText
            
            nameLabel.text = viewModel?.status.user?.screen_name
            
            memberView.image = viewModel?.iconImage
            
            vipIconView.image = viewModel?.vipIcon
            
            iconView.lau_image(urlString: viewModel?.status.user?.profile_image_url, placeholderImage: UIImage(named: "avatar_default_big"),isAvatar: true)
            
            statusBar.viewModel = viewModel
            
            /// 设置配图是图底部到工具栏的约束
//            pictureView.pictureViewBottomCons.constant = viewModel?.statusPictureSize.height ?? 0
            pictureView.viewModel = viewModel
            
            pictureView.urls = viewModel?.picURLS
            
            
            
            //微博来源的赋值
            sourceLabel.text = viewModel?.status.source
        }
    }
    
    /// 头像
    @IBOutlet weak var iconView: UIImageView!
    /// 姓名
    @IBOutlet weak var nameLabel: UILabel!
    /// 会员
    @IBOutlet weak var memberView: UIImageView!
    /// 时间
    @IBOutlet weak var timeLabel: UILabel!
    /// 来源
    @IBOutlet weak var sourceLabel: UILabel!
    /// 认证图片
    @IBOutlet weak var vipIconView: UIImageView!
    /// 正文
    @IBOutlet weak var statusLabel: LauLabel!
    /// 工具栏
    @IBOutlet weak var statusBar: WBStatusToolbar!
    /// 配图视图
    @IBOutlet weak var pictureView: WBStatusPictureView!

    /// 转发微博的label 这个一定是可选的才行,要不然容易崩溃,因为原创微博并没与这个label
    @IBOutlet weak var reteewedLabel: LauLabel?
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        /// 离屏渲染
        /*
         如果检测到cell的性能已经很好,就不需要做离屏渲染,
         因为:离屏渲染需要在 CPU / GPU 之间快速切换,耗电很厉害
         */
        self.layer.drawsAsynchronously = true
        
        /// 栅格化
        /*
         异步绘制之后,会生成一张独立的图像,cell在屏幕上滚动的时候,本质上滚动的是这张图片
         cell优化,要尽量减少图层的属相,相当于就一层,
         体制滚动之后,可以接受监听
         */
        self.layer.shouldRasterize = true
        /// 如果使用了 栅栏化 一定要制定分辨率,要不然会很模糊
        self.layer.rasterizationScale = UIScreen.main.scale
        
        statusLabel.delegate = self
        reteewedLabel?.delegate = self
        
    }
}

// MARK: - LauLabelSelectedDelegate
extension WBStatusCell: LauLabelSelectedDelegate{
    
    // MARK: -- 实现LauLabel的代理方法
    func didSelectedUrlString(label: LauLabel, text: String) {
        
        //如果不是URL,直接返回
        if !text.hasPrefix("http://") {
            
            return
        }
        
        delegate?.didSelectedStatusCell?(statusCell: self, url: text)
    }

}
